package com.jiezuo.flowable.app;

import com.google.common.collect.Lists;
import com.jiezuo.flowable.service.MyService;
import org.flowable.ui.common.model.GroupRepresentation;
import org.flowable.ui.common.model.ResultListDataRepresentation;
import org.flowable.ui.common.model.UserRepresentation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 *@Description  重写Flowable 下拉用户
 *    flowable-ui-modeler-rest-6.5.0.jar->org.flowable.ui.modeler.rest->EditorUsersResource
 *@Author 庞留杰
 *@Date2020/9/14 10:14
 *@Version V1.0
 **/

@RestController
@RequestMapping("/app")
public class EditorUsersResource {

    @Autowired
    MyService myService;

    /**
     * 功能描述: 用于Flowable Modeler用户下拉
     * @author: 庞留杰
     * @exception
     * @date: 2020/9/9 17:01
     */
    @RequestMapping(value = "/rest/editor-users", method = RequestMethod.GET)
    public ResultListDataRepresentation getUsers(@RequestParam(value = "filter", required = false) String filter) {
       /* 查询FLowable自带表act_id_user
            if (StringUtils.isNotBlank(filter)) {
                filter = filter.trim();
                String sql = "select * from act_id_user where ID_ like #{id} or LAST_ like #{name}";
                filter = "%"+filter+"%";
                List<User> matchingUsers = idmIdentityService.createNativeUserQuery().sql(sql).parameter("id",filter).parameter("name",filter).listPage(0, 10);List<UserRepresentation> userRepresentations = new ArrayList<>(matchingUsers.size());
                for (User user : matchingUsers) {
                    userRepresentations.add(new UserRepresentation(user));
                }
                return new ResultListDataRepresentation(userRepresentations);
            }
            return null;
        */
        // 此处重写
        List<Map<String, Object>> users = myService.findUsersAndButtonIds(filter);
        List<Map<String, Object>> buttons = myService.findButtons(null);
        List<UserRepresentation> userRepresentations = new ArrayList();
        for (Map user:users) {
            String real_name = user.get("real_name")+"";
            UserRepresentation userRepresentation = new UserRepresentation();
            userRepresentation.setId(user.get("user_id")+"");
            userRepresentation.setFullName(real_name);
            //姓，名
            if(real_name != null && real_name.length() >= 1){
                String firstName=real_name.substring(1,real_name.length());//名字
                String lastName=real_name.substring(0,1);//姓
                userRepresentation.setFirstName(firstName);
                userRepresentation.setLastName(lastName);
            }else {
                userRepresentation.setFirstName("");
                userRepresentation.setLastName("");
            }
            userRepresentation.setEmail(user.get("email")+"");
            List<GroupRepresentation> groups = Lists.newArrayList();
            for(Map button:buttons){
                String[] role_ids = (user.get("button_ids") + "").split(",");
                if(Arrays.asList(role_ids).contains(button.get("id")+"")){
                    GroupRepresentation groupRepresentation = new GroupRepresentation();
                    groupRepresentation.setId(button.get("id")+"");
                    groupRepresentation.setName(button.get("resource_name")+"");
                    groupRepresentation.setType(null);
                    groups.add(groupRepresentation);
                }
            }
            userRepresentation.setGroups(groups);
            userRepresentations.add(userRepresentation);
        }
        return new ResultListDataRepresentation(userRepresentations);
    }
}
